package cn.tedu.csmall.product.service.impl;

import cn.tedu.csmall.product.ex.ServiceException;
import cn.tedu.csmall.product.mapper.SkuSpecificationMapper;
import cn.tedu.csmall.product.pojo.dto.SkuSpecificationAndNewDTO;
import cn.tedu.csmall.product.pojo.entity.SkuSpecification;
import cn.tedu.csmall.product.pojo.vo.SkuStandardVO;
import cn.tedu.csmall.product.service.ISkuSpecificationService;
import cn.tedu.csmall.product.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Slf4j
@Service
public class SkuSpecificationServiceImpl implements ISkuSpecificationService, ServiceCode {
    @Autowired
    SkuSpecificationMapper skuSpecificationMapper;
    public SkuSpecificationServiceImpl() {
        log.debug("创建业务类:SkuSpecificationServiceImpl");
    }
    @Override
    public void andNew(SkuSpecificationAndNewDTO skuSpecificationAndNewDTO) {
        log.debug("开始处理添加功能，参数：{}",skuSpecificationAndNewDTO);
        SkuSpecification skuSpecification=new SkuSpecification();
        LocalDateTime now= LocalDateTime.now();
        BeanUtils.copyProperties(skuSpecificationAndNewDTO,skuSpecification);
        skuSpecification.setGmtCreate(now);
        skuSpecification.setGmtModified(now);
        int insert = skuSpecificationMapper.insert(skuSpecification);
        if (insert!=1){
            String message="添加数据失败！服务器忙，请稍后再一次尝试!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }
    }
    @Override
    public void delete(Long id) {
    log.debug("开始处理删除命令");
        SkuStandardVO skuStandard = skuSpecificationMapper.getSkuStandard(id);
        log.debug("删除数据！参数:{}",skuStandard);
        if (skuStandard==null){
            String message="删除数据失败！"+id+"该数据不存在";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        int i = skuSpecificationMapper.delete(id);
        if (i!=1){
            String message="删除数据失败！服务器忙，请稍后再一次尝试!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_DELETE,message);
        }
    }
}
